﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MetroFramework.Controls;

namespace ComUIControl
{
    public partial class MinmaxValBox : MetroUserControl, INotifyPropertyChanged
    {
        private string _valName = "name";
        public string ValName
        {
            get
            {
                return _valName;
            }
            set
            {
                metroLabel1.Text = value;
                _valName = value;
            }
        }
        public MinmaxValBox()
        {
            InitializeComponent();
            metroTextBoxMin.DataBindings.Add("Text", this, "MinVal");
            metroTextBoxMax.DataBindings.Add("Text", this, "MaxVal");
        }

        private double _minVal = 0D;
        [DefaultValue(0D)]
        public double MinVal
        {
            get
            {
                return _minVal;
            }
            set
            {
                if (value != _minVal)
                {
                    RaisePropertyChanged("MinValue");
                }
                _minVal = value;
                if (metroTextBoxMin != null)
                {
                    metroTextBoxMin.Text = value.ToString();
                }
            }
        }

        private double _maxVal = 0D;
        [DefaultValue(0D)]
        public double MaxVal
        {
            get
            {
                return _maxVal;
            }
            set
            {
                if (value != _maxVal)
                {
                    RaisePropertyChanged("MaxVal");
                }
                _maxVal = value;
                if (metroTextBoxMax != null)
                {
                    metroTextBoxMax.Text = value.ToString();
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public delegate void UpdatePropertyDelegate(string strName);
        private void RaisePropertyChanged(string propertyName)
        {
            if (this.InvokeRequired)
            {
                UpdatePropertyDelegate outdelegate = new UpdatePropertyDelegate(RaisePropertyChanged);
                this.BeginInvoke(outdelegate, new object[] { propertyName });
                return;
            }
            var handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private void metroTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\b'
                || Char.IsDigit(e.KeyChar)
                || Char.IsControl(e.KeyChar)
                || e.KeyChar == 46)
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void metroTextBoxMin_Validating(object sender, CancelEventArgs e)
        {
            double val;
            if (double.TryParse(metroTextBoxMin.Text, out val))
            {
                if (val > _maxVal)
                {
                    string infoErr = string.Format("最小值不能大于最大值, 请输入一个小于等于{0}的数.", _maxVal);
                    errorProvider1.SetError(metroTextBoxMin, infoErr);
                }
                else
                {
                    //Value = val;
                    errorProvider1.SetError(metroTextBoxMin, "");
                }
            }
            else
            {
                errorProvider1.SetError(metroTextBoxMin, "不是合法的数字!");
                metroTextBoxMin.Focus();
            }
        }

        private void metroTextBoxMax_Validating(object sender, CancelEventArgs e)
        {
            double val;
            if (double.TryParse(metroTextBoxMax.Text, out val))
            {
                if (val < _minVal)
                {
                    string infoErr = string.Format("最大值不能小于最小值, 请输入一个大于等于{0}的数.", _minVal);
                    errorProvider1.SetError(metroTextBoxMax, infoErr);
                }
                else
                {
                    //Value = val;
                    errorProvider1.SetError(metroTextBoxMax, "");
                }
            }
            else
            {
                errorProvider1.SetError(metroTextBoxMax, "不是合法的数字!");
                metroTextBoxMax.Focus();
            }
        }

    }
}
